(function(){
    var drag = {
        init: function(id,callBack) {
            this.id = id
            this.callBack = callBack
            this.flag = false;
            this.dragEle = null;
            this.cur = {
                x:0,
                y:0
            }
            this.nx = this.ny = this.dx = this.dy = this.x = this.y = 0;
            this.dragWidth = this.dragHeight = 0;
            
            this.timmerHandle = null;

            this.setListener();
        },
        setListener: function() {
            var _this = this
            this.dragEle = document.getElementById(this.id);

            //-------------------PC端-------------------------------------------
            this.dragEle.addEventListener("mousedown",function(){
                event.preventDefault();
                _this.down();
            },false);
            
            this.dragEle.addEventListener("mousemove",function(){
                event.preventDefault();
                _this.move();
            },false);
           
            document.body.addEventListener("mouseup",function(){
                event.preventDefault();
                _this.end();
            },false);
            //-------------------移动端-------------------------------------------
            this.dragEle.addEventListener("touchstart",function(){
                event.preventDefault();
                _this.down();
            },false);

             this.dragEle.addEventListener("touchmove",function(){
                event.preventDefault();
                _this.move();
            },false);

            this.dragEle.addEventListener("touchend",function(){
                event.preventDefault();
                _this.end();
            },false);
        },
        down: function() { // 按下鼠标或手指触碰到屏幕时执行
            var _this = this
            this.timmerHandle = setTimeout(function(){
                _this.flag = true;
            },200);
            var touch ;
            if(event.touches){
                touch = event.touches[0];
            }else {
                touch = event;
            }
            this.cur.x = touch.clientX;
            this.cur.y = touch.clientY;
            this.dx = this.dragEle.offsetLeft;
            this.dy = this.dragEle.offsetTop;
            this.dragWidth = this.dragEle.offsetWidth;
            this.dragHeight = this.dragEle.offsetHeight;
            
        },
        move: function() { // 拖动时执行
            if(this.flag){
                var touch ;
                if(event.touches){
                    touch = event.touches[0];
                }else {
                    touch = event;
                }
                this.nx = touch.clientX - this.cur.x;
                this.ny = touch.clientY - this.cur.y;
                this.x =  this.dx+ this.nx;
                this.y =  this.dy+ this.ny;
                this.isDrag()
            }
        },
        end: function() { //释放时执行的函数
            if(!this.flag){ // 判断是点击还是拖动
                this.callBack(event)
            }
            this.flag = false;
            clearTimeout(this.timmerHandle);
            
        },
        isDrag: function() { // 判断是否超出拖动范围(屏幕)
            // var minX = minY = 0;
            // var maxX = document.documentElement.clientWidth -this.dragWidth;
            // var maxY = document.documentElement.clientHeight - this.dragHeight;
            
            // this.x = this.x < minX ? minX : this.x
            // this.x = this.x > maxX ? maxX : this.x
            // this.y = this.y < minY ? minY : this.y
            // this.y = this.y > maxY ? maxY : this.y

            this.setPosition()
        },
        setPosition: function() { // 改变位置
            this.dragEle.style.left = this.x+"px";
            this.dragEle.style.top = this.y +"px";
        }
    }
    window.drag = drag;     
})(window)